import time
from lxml import etree
import aiohttp
import asyncio
# 异步协程请求+多进程解析（推荐）
urls = [
    'https://blog.csdn.net/qq_40924514/article/details/112705877',
    'https://blog.csdn.net/qq_40924514/article/details/112606141',
    'https://blog.csdn.net/qq_40924514/article/details/112587048',
    'https://blog.csdn.net/qq_40924514/article/details/112587048',
    'https://blog.csdn.net/qq_40924514/article/details/110232800',
    'https://blog.csdn.net/qq_40924514/article/details/109773234',
    'https://blog.csdn.net/qq_40924514/article/details/109694920',
    'https://blog.csdn.net/qq_40924514/article/details/109587689',
    'https://blog.csdn.net/qq_40924514/article/details/109496196',
    'https://blog.csdn.net/qq_40924514/article/details/109405649',
]

title = []
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'}
# 设置信号量，控制协程数，放置爬的过快
sem = asyncio.Semaphore(4)

async  def get_title(url):
    async with(sem):
        async with aiohttp.ClientSession() as session:
            async with session.request('GET', url, headers=headers) as resp:
                html = await resp.read()
                title = etree.HTML(html).xpath('//title/text()')
                print(''.join(title))

def run():
    # 创建任务列表
    tasks = [get_title(url) for url in urls]
    # 创建事件循环对象
    loop = asyncio.get_event_loop()
    # 开启事件循环，激活协程   asyncio.wait(tasks)--以列表进行注册
    loop.run_until_complete(asyncio.wait(tasks))
    # 关闭事件循环
    loop.close()

if __name__ == '__main__':
    start = time.time()
    run()
    print('共耗时：', time.time()-start)
